<template>
	<view>
		<r-canvas ref="rCanvas" style="visibility: hidden;height: 1upx;"></r-canvas>
		<!-- <image src="file:///storage/emulated/0/Android/data/cn.puyunwl.mindclass/apps/__UNI__B98E661/doc/0.6292298726274739.jpg"></image> -->
		<view class="qingding" style="height: auto !important;">
			<text class="qd_tt">学期作业综合报告</text>
			<text class="qd_tt" style="font-size: 30upx;padding-top: 20upx;">{{cj.term_name}}</text>
			<!-- <text class="qd_xm">姓名：{{cj.stuName}}</text> -->
			<view style="height: 30upx;"></view>
			<view class="qd_xqs">
				<text class="m-t2" style="font-size: 26upx;">学期名称：{{ cj.workTerm }}</text>
				<text class="m-t2" style="font-size: 26upx;">指导老师：{{ cj.teaName }}</text>
				<text class="m-t2" style="font-size: 26upx;">接收的作业量：{{cj.reciveCount || 0}}</text>
				<text class="m-t2" style="font-size: 26upx;">完成的作业量：{{cj.doneCount || 0}}</text>
			</view>
			<view class="qd_xqs">
				<text class="m-t2" style="font-size: 26upx;font-weight: normal;">{{cj.stuName}}</text>
				<text class="m-t2" style="font-size: 26upx;font-weight: normal;">{{cj.depa}}</text>
				<text class="m-t2" style="font-size: 26upx;font-weight: normal;">{{cj.prof}}</text>
				<text class="m-t2" style="font-size: 26upx;font-weight: normal;">{{cj.grade}}级  {{cj.className}}班</text>
				<text class="m-t2" style="font-size: 26upx;font-weight: normal;">学号：{{cj.stuNo}}</text>
			</view>
			<view class="qys" style="margin-top: 50upx;padding-bottom: 120upx;" >
				<text class="m-t2">{{cj.comment}}</text>
				<view style="height: 50upx;"></view>
				<text class="wcl-3 f-w" >思政学期作业综合成绩：{{cj.score}}分</text>
				<view class="f pingfenxx">
					<text class="f-w" style="margin-top: 2upx;">能力评定：</text>
					<view class="xx" v-for="(item,index) in starNum">
						<image src="../../../static/one_icon.png" mode="widthFix"></image>
					</view>
				</view>
			</view>
		</view>
		<view class="style2" @tap="createImg()">保存报告</view>
	</view>
</template>

<script>
	import rCanvas from "@/components/r-canvas/r-canvas.vue"
	import permision from "@/js_sdk/wa-permission/permission.js"
	// 以下路径需根据项目实际情况填写
	import { pathToBase64, base64ToPath } from '../../../common/index.js'
	export default {
		  components:{
		        rCanvas
		    },
		data() {
			return {
				id: [],
				cj: [],
				type: 50,
				show: false,
				show1: false,
				starNum: 0, //综合评定星星数量
				ratio: getApp().globalData.ratio,
			}
		},
		onReady() {
			// this.createImg();
		},
		onShow() {

		},
		onLoad(option) {
			this.termWrokId = option.termWorkId;
		},
		created() {
			this.init()
			let that = this
			let params = {
				"termWrokId": that.termWrokId
			}
			that.pyReq(that.$http.getTermWorkReportStu, params, function(res) {
				that.cj = res.result
				console.log(that.cj);
				let sumScore = that.cj.score;
			        if(sumScore>=90){
			          that.starNum = 5;
			        }else if(sumScore>=80){
			          that.starNum = 4;
			        }else if(sumScore>=70){
			          that.starNum = 3;
			        }else if(sumScore>=60){
			          that.starNum = 2;
			        }else{
			          that.starNum = 1;
			        }
						// if(sumScore>=1)
						 // else if(sumScore==0){
			          // that.starNum = 0;
			        // }
			});
		},
		methods: {
			createImg(){
				let platform=uni.getSystemInfoSync().platform
				// if(platform=='ios'){
				// 	  console.log('我是iOS')
				// 	return  this.toast('暂不支持ios设备')
				// }
				uni.showLoading({
					title: '报告生成中...'
				});
				let x = 0-35/this.ratio*uni.getSystemInfoSync().pixelRatio;
				let y = 0-((uni.getSystemInfoSync().screenHeight-uni.getSystemInfoSync().windowHeight)*uni.getSystemInfoSync().pixelRatio+30/this.ratio*uni.getSystemInfoSync().pixelRatio)+10;
				let w = uni.getSystemInfoSync().windowWidth*uni.getSystemInfoSync().pixelRatio;
				let h = uni.getSystemInfoSync().screenHeight*uni.getSystemInfoSync().pixelRatio;
				let picRatio = 1758/1000;
				let cw = w + x + x;
				let ch = cw*picRatio;
				let _this = this
				let pages = getCurrentPages();
				let page = pages[pages.length - 1];
				let bitmap = null;
				let currentWebview = page.$getAppWebview();
				bitmap = new plus.nativeObj.Bitmap('amway_img');
				// 将webview内容绘制到Bitmap对象中
				currentWebview.draw(bitmap, function() {
					// bitmap.save( "_doc/"+Math.random()+".jpg"
					bitmap.save("_doc/" + Math.random() + ".jpg", {}, function(i) {
						let file = i.target;
						
						setTimeout(function(){
							
							_this.$nextTick(async ()=>{
							        // 初始化
							        await _this.$refs.rCanvas.init({
							            canvas_id:"rCanvas",
										canvas_width:cw,
										canvas_height:ch,
										hidden:true
							        })
							        // 画图
							        await _this.$refs.rCanvas.drawImage({
							            url:file,
							            x:x,
							            y:y,
							            w:w,
							            h:h
							        }).catch(err_msg=>{
							            uni.showToast({
							                title:err_msg,
							                icon:"none"
							            })
							        })
							        // 生成海报
							        await _this.$refs.rCanvas.draw((res)=>{
							            //res.tempFilePath：生成成功，返回base64图片
							            // 保存图片
										let platform=uni.getSystemInfoSync().platform
										if(platform=='ios'){
											uni.canvasToTempFilePath({
												canvasId: 'rCanvas',
												success: function(res) {
													new Promise((resolve, reject) => {
														var PHPhotoLibrary = plus.ios.import("PHPhotoLibrary");
														var status = PHPhotoLibrary.authorizationStatus();
														if (status == 3) {
															_this.toast('报告已保存到相册');
															resolve()
														} else {
															uni.hideLoading();
															plus.nativeUI.confirm("相册权限没有开启，是否去开启？", function(e) {
																if (e.index == 0) {
																	var UIApplication = plus.ios.import("UIApplication");
																	var application2 = UIApplication.sharedApplication();
																	var NSURL2 = plus.ios.import("NSURL");
																	var setting2 = NSURL2.URLWithString("app-settings:");
																	application2.openURL(setting2);
																	plus.ios.deleteObject(setting2);
																	plus.ios.deleteObject(NSURL2);
																	plus.ios.deleteObject(application2);
																}
															}, {
																"buttons": ["去开启", "暂不开启"],
																"verticalAlign": "center"
															});
															// reject()
														}
														plus.ios.deleteObject(PHPhotoLibrary);
													})
													
												},
											})
										}else if(platform=='android'){
											  _this.$refs.rCanvas.saveImage(res.tempFilePath)
											  _this.toast('报告已保存到相册');
										}
							            
							        }).then(res => {}).catch(err => {
										console.log("err", err);
									})
							    })
							
						},1000);
					}, function(e) {
						// console.log('保存图片失败：' + JSON.stringify(e));
					});
				}, function(e) {
					// console.log('截屏绘制图片失败：' + JSON.stringify(e));
				});
				
				
			}
			,
			init() {
				var that = this;
				if (that.type <= 20 || that.type >= 20 && that.type <= 30) {
					that.show = true
				} else if (that.type <= 30 || that.type >= 40 && that.type <= 50) {
					that.show1 = true
				}
			}
		}
	}
</script>

<style>
	.style2{
		margin-top: 40upx;
		float: right;
		background-color: #B31711;
		border-radius: 6upx;
		color: #fff;
		font-size: 24upx;
		padding: 10upx 40upx;
		margin-right: 40upx;
	}
	.xx {
		text-align: left;
	}

	.xx image {
		width: 28upx;
		margin-right: 10upx;
	}

	.qys {
		width: 500upx;
		margin: 0 auto;
		margin-top: 40upx;
	}

	.pingfenxx {
		margin-top: 20upx;
		/* justify-content: space-between; */
		font-weight: 700;
	}

	.qys text {
		font-size: 24upx;
		color: #BB3832;
		display: block;
		line-height: 40upx;
	}

	.qys .wcl-3 {
		/* font-size: 30upx; */
		font-weight: 700;
	}

	.qd_xqlis {
		width: 240upx;
		margin-top: 15upx;
		display: block;
		width: 100%;
	}

	.qd_xqlis text {
		font-size: 24upx;
		color: #000000;
	}

	.qd_xq {
		width: 500upx;
		margin: 0 auto;
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
		margin-top: 12upx;
		border-bottom: 1upx solid #955209;
		padding-bottom: 20upx;
	}

	.qd_xq text {
		font-size: 24upx;
		color: #BB3832;
	}

	.qd_xqs {
		width: 500upx;
		margin: 0 auto;
		margin-top: 40upx;
		border-bottom: 1upx solid #955209;
		padding-bottom: 20upx;
		font-size: 24upx;
	}

	.qd_xqs text {
		font-weight: 700;
		display: block;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	.qingding {
		margin-top: 30upx;
		margin-left: 35upx;
		width: 680upx;
		height: 1083upx;
		background-image: url(/static/baogao.png);
		background-position: center;
		background-size: 100% 100%;
	}

	.qd_tt {
		display: block;
		text-align: center;
		font-size: 42upx;
		color: #B31610;
		padding-top: 127upx;
		font-weight: 700;
	}

	.qd_xm {
		display: block;
		text-align: center;
		border: 1upx solid #9D601C;
		color: #9D601C;
		width: 250upx;
		height: 50upx;
		line-height: 50upx;
		font-size: 30upx;
		margin: 0 auto;
		border-radius: 30upx;
		margin-top: 30upx;
	}
</style>
